class CellHeaderView extends egret.DisplayObjectContainer{
    private _bg:egret.Bitmap;

    private _iconImg:eui.Image;
    private _levelImg:egret.Bitmap;
    private _headerImg:egret.Bitmap; // 显示群主

    private _level:number; // 等级
    private _iconURL:string; // 头像地址

    private _isHeader:boolean; // 是否是群主


    constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.viewDidLoad, this);
    }

    private viewDidLoad(){
        // 背景
        if(!this.contains(this.bg)) this.addChild(this.bg);
        // 头像
        if(!this.contains(this.iconImg)) this.addChild(this.iconImg);
        // 等级
        if(!this.contains(this.levelImg)) this.addChild(this.levelImg);
        
    }

    /*********************************************** setter **********************************************************/
    public set level(value: number) {
        this._level = value;
        this.levelImg.texture = RES.getRes('levelname' + value);
        this.bg.texture = RES.getRes('level' + value);
    }
    
    public set iconURL(value: string) {
        this._iconURL = value;
        this.iconImg.source = value;
    }
    
    public set isHeader(value: boolean) {
        this._isHeader = value;
        if(value){
            if(!this.contains(this.headerImg)) this.addChild(this.headerImg);
        }else{
            if(this.contains(this.headerImg)) this.removeChild(this.headerImg);
        }
    }

    /*********************************************** getter **********************************************************/
    
    private get headerImg():egret.Bitmap{
        if(!this._headerImg){
            var headerImg = utils.createBitmapByName('bz', {width:this.width * 0.6});
            this._headerImg = headerImg;
        }
        return this._headerImg;
    }

    public get iconImg():eui.Image{
        if(!this._iconImg){
            var iconImg = new eui.Image();
            iconImg.source = RES.getRes(this.iconURL || 'face');
            var margin = 6;
            iconImg.width = this.width - 2 * margin;
            iconImg.height = this.height - 2 * margin;
            iconImg.x = margin;
            iconImg.y = margin;
            var mask = utils.drawRound(iconImg.x, iconImg.y, iconImg.width, iconImg.height, 0x000, 10);
            this.addChild(mask);
            iconImg.mask = mask;
            this._iconImg = iconImg;
        }
        return this._iconImg;
    }

    public get bg():egret.Bitmap{
        if(!this._bg){
            var bg = new egret.Bitmap();
            var level = this.level || 1;
            bg.texture = RES.getRes('level' + level);
            bg.width = this.width;
            bg.height = this.height;
            this._bg = bg;
        }
        return this._bg;
    }

    public get levelImg():egret.Bitmap{
        if(!this._levelImg){
            var level = this.level || 1;
            var levelImg = utils.createBitmapByName('levelname' + level, {width:this.width + 20});
            levelImg.y = this.height - levelImg.height;
            this._levelImg = levelImg;
        }
        return this._levelImg;
    }
}